/**
 * Created by Administrator on 2017/7/20.
 */
import {tyBase64} from './md5base64'
var tyIsBase64 = tyBase64()
var ty_throw_msg = function (ty_msg) {
 return {
   developer: 'yangBin',
   error: ty_msg
 }
};
var ty_type_arr = ['sessionStorage', 'localStorage'];
var storage = {};
(function (window,undefined) {
    function getStorage(ty_type) {
        return function (ty_string, ty_status) {
            if(typeof ty_string !== 'string'){
                throw new ty_throw_msg('操作的key只能是字符串');
                return
            }
            var ty_get_val = window[ty_type].getItem(ty_string) ? window[ty_type].getItem(ty_string) : '';
            ty_get_val = ty_status === true && ty_get_val  ? tyIsBase64.decode(ty_get_val) : ty_get_val;
            try {
                ty_get_val = JSON.parse(ty_get_val)
            }catch(err){
                // throw new ty_throw_msg(err);
            }
            return ty_get_val
        }
    }
    function setStorage(ty_type) {
        return function (ty_key, ty_obj, ty_status) {
            if(typeof ty_key !== 'string'){
                throw new ty_throw_msg('操作的key只能是字符串');
                return
            }
            ty_obj = typeof ty_obj === 'object' ? JSON.stringify(ty_obj) : (
                ty_obj === null || ty_obj === undefined ? '' : ty_obj
            );
            ty_obj = ty_status === true ? tyIsBase64.encode(ty_obj) :ty_obj;
            window[ty_type].setItem(ty_key + '', ty_obj);
        }
    }
    function removeStorage(ty_type) {
        return function (ty_arr) {
            if(typeof ty_arr === 'string'){
                window[ty_type].removeItem(ty_arr)
            }
            else if(Object.prototype.toString.call(ty_arr) === '[object Array]'){
                for(var ini=0;ini<ty_arr.length;ini++){
                    window[ty_type].removeItem(ty_arr[ini])
                }
            }
            else if(typeof ty_arr ==='undefined' || ty_arr=== null){
                window[ty_type].clear();
            }
            else {
                throw new ty_throw_msg('参数只能是数组、字符串、空')
            }
        }
    }
    function buildCookie(window,document,cookie) {
        return{
            set:function (ty_name,ty_val,ty_time)
            {
                ty_time =typeof ty_time !== 'number' ? 1000 :ty_time;
                var ty_exp = new Date();
                ty_exp.setTime(ty_exp.getTime() + ty_time*60*60*1000);
                if(typeof ty_name !== 'string'){
                    throw new ty_throw_msg('操作的key只能是字符串');
                    return
                }
                ty_val = typeof ty_val ==='object'? JSON.stringify(ty_val) : (
                    ty_val ===null || ty_val === undefined ? '' : ty_val
                )
                ty_val = arguments[arguments.length-1] === true ? tyIsBase64.encode(ty_val): ty_val
                window[document][cookie] = ty_name + "="+ escape (ty_val) + ";expires=" + ty_exp.toGMTString();
            }
            ,

//读取cookies
            get:function (ty_name, ty_status)
            {
                if(typeof ty_name !== "string"){
                    throw new ty_throw_msg('操作的key只能是字符串');
                    return
                }
                if (window[document][cookie].length>0)
                {
                    var ty_start=window[document][cookie].indexOf(ty_name + "=")
                    if (ty_start!=-1)
                    {
                        ty_start=ty_start + ty_name.length+1
                        var ty_end=window[document][cookie].indexOf(";",ty_start)
                        if (ty_end==-1) ty_end=window[document][cookie].length
                        var getValue=unescape(window[document][cookie].substring(ty_start,ty_end))
                        getValue = ty_status ===true ? tyIsBase64.decode(getValue) :getValue
                        try {
                            getValue=JSON.parse(getValue)
                        }
                        catch(m){

                        }

                        return getValue
                    }
                }
                return false
            }
            ,
//删除cookies
            remove: function (ty_name)
            {
                var ty_exp = new Date();
                ty_exp.setTime(ty_exp.getTime() - 10);
                if(typeof ty_name ==='string'){
                    var ty_val=this.get(ty_name);
                    if(ty_val!==false && ty_val!==null)
                    {window[document][cookie]= ty_name + "="+ty_val+";expires="+ty_exp.toGMTString();}
                }
                else if(Object.prototype.toString.call(ty_name) === '[object Array]'){
                    ty_name.map(function (ty_item) {
                        var ty_val=this.get(ty_item);
                        if(ty_val!==false && ty_val!==null)
                        {window[document][cookie]= ty_name + "="+ty_val+";expires="+ty_exp.toGMTString();}
                    })
                }
                else {
                    throw new ty_throw_msg('参数只能是数组、字符串、空')
                }

            }
        }
    }
    ty_type_arr.map(function (ty_item) {
        storage[ty_item] = {};
        storage[ty_item]['get'] = getStorage(ty_item);
        storage[ty_item]['set']= setStorage(ty_item);
        storage[ty_item]['remove'] = removeStorage(ty_item);
    });
    storage['cookie'] = new buildCookie(window, 'document', 'cookie')


})(window,undefined)
export default storage
